package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: DeptController
 * Package: com.qk.controller
 * Description:
 *
 * @Author zt
 * @Create 2025/7/27 9:09
 * @Version 1.0
 */
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;
    /*
    * 新增部门
     */
    @PostMapping
    @Log
    public Result add(@RequestBody Dept dept) {
       log.info("新增部门{}" , dept);
        deptService.add(dept);
        return Result.success();
    }

    /*
    列表查询
     */
    @GetMapping
    public Result list(String name, Integer status,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        //System.out.println("列表查询：" + name + " " + status + " " + page + " " + pageSize);
        log.info("条件分页查询部门: name={}, status={}, page={}, pageSize={}", name, status, page, pageSize);

        //调用 service，分页查询
        PageResult<Dept> pageResult = deptService.listDepts(name, status, page, pageSize);
        return Result.success(pageResult);
    }
    /*
    根据id查询部门
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("根据ID查询部门: {}" , id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /*
    修改部门
     */
    @PutMapping
    @Log
    public Result update(@RequestBody Dept dept) {
       log.info("修改部门: {}" , dept);
        deptService.update(dept);
        return Result.success();
    }

    /*
    根据id删除部门
     */
    @DeleteMapping("/{id}")
    @Log
    public Result delete(@PathVariable Integer id) {
        log.info("删除部门: {}" , id);
        deptService.delete(id);
        return Result.success();
    }

    /*
    查询所有部门
     */
    @GetMapping("/list")
    public Result list() {
        //log.info("查询所有部门");
        //展示的应该是数据列表，返回值用集合来接受
        List<Dept> list = deptService.list();
        return Result.success(list);
    }

}
